<template>
  <view>
    <cu-custom bgColor="bg-gradual-green" :isBack="true">
      <block slot="backText">返回</block>
      <block slot="content">FMS测试</block>
    </cu-custom>
    <view style="margin-bottom: 110rpx;">
      <view class="mbttom15">
        <view class="fms-item u-f-ac relative" v-for="(item, index) in list" :key="index" @click="goPage(item)">
          <image :src="item.img" />
          <view>{{item.title}}</view>
          <view class="abs-emoji u-f-ac u-f-jsb">
            <view v-if="item.fmsDetails">
              <text v-if="item.fmsDetails.input == 2">（{{item.fmsDetails.input1}}/{{item.fmsDetails.input2}}分）</text>
              <text style="margin: 0 10rpx 0 14rpx;">{{item.countScores}}分</text>
            </view>
            <view class="cuIcon-right"></view>
          </view>
        </view>

      </view>
      <view>
        <textarea placeholder="请输入总体评价" v-model="remark" placeholder-style="color: #ccc;" />
        </view>
    </view>

    <button type="default" class="bottom-btn u-f-ajc" @click="submit">生成报告</button>



  </view>
</template>

<script>
  import timeJS from "../../common/time.js"
  export default {
    data() {
      return {
        remark: '',
        list: [
          {
            img: '../../static/BasicsBg.png',
            title: '深蹲',
            input: 1,
          },
          {
            img: '../../static/BasicsBg.png',
            title: '过栏架步',
            input: 2,
          },
          {
            img: '../../static/BasicsBg.png',
            title: '前后分腿蹲',
            input: 2,
          },
          {
            img: '../../static/BasicsBg.png',
            title: '肩部灵活伸出',
            input: 2,
          },
          {
            img: '../../static/BasicsBg.png',
            title: '主动直膝抬腿',
            input: 2,
          },
          {
            img: '../../static/BasicsBg.png',
            title: '躯干稳定性俯撑',
            input: 1,
          },
          {
            img: '../../static/BasicsBg.png',
            title: '转动稳定性',
            input: 2,
          },
        ],
      }
    },
    onShow() {
      let fmsDetails = uni.getStorageSync('fmsDetails')
      if(fmsDetails && fmsDetails.length) {
        fmsDetails.forEach((item, index) => {
          this.list.forEach((item2, index2) => {
            if (item.title == item2.title) {
              this.$set(item2, 'fmsDetails', item);
              this.$set(item2, 'countScores', parseInt(this.comput(item.input1, item.input2)));
            }
          })
        })
      }
    },

    methods: {
      countScores() {
        // return this.list.filter(item => item.countScores).map(item => item.countScores).reduce((pre, cur) => pre + cur)
        let arrs = this.list.filter(item => item.hasOwnProperty('countScores')).map(item => item.countScores)
        console.log(arrs)
        console.log(arrs.some(item => item > 0))
        
        if (arrs.some(item => item > 0)) {
          console.log('每项都不为0')
           return arrs.reduce((pre, cur) => pre + cur)
        }
        else return 0
      },
      comput(input1, input2) {
        if (input2) { // 如果第二项存在
          if (input1 > input2) return input2 // 如果第一项比二项大的话 返回第二项
          else return input1 // 否则返回第一项
        } else { // 不存在返回第一项
         return input1 
        }
      },
      goPage(item) {
        let url = `./create_fms_details?title=${item.title}&input=${item.input}`;
        uni.redirectTo({
          url,
        });
      },
      async submit() {
        if (!this.list.every(item => item.hasOwnProperty('countScores'))) return this.$showToast('请全部完成~您提交的FMS测试数据不全~~~', 'none')
        
        let objs = (titles) => {
          return {
            "name": this.list.find(item => item.title == titles).fmsDetails.title,
            "leftScores": this.list.find(item => item.title == titles).fmsDetails.input1,
            "remark": this.list.find(item => item.title == titles).fmsDetails.remark,
            "rightScores": this.list.find(item => item.title == titles).fmsDetails.input2,
            "veido": this.list.find(item => item.title == titles).fmsDetails.picUrl.length ? this.list.find(item => item.title == titles).fmsDetails.picUrl[0] : '',
            "scores": 0,
            "yang": 0,
            
          }
        }
        
        let data = {
              "bId": 1,
              "countRemark": this.remark,
              "countScores": this.countScores(),
              "gymId": 0,
              "liftLegsOnKnees": objs('主动直膝抬腿'),
              "memberId": uni.getStorageSync('phyUser').userId,
              "rotationalStability": objs('转动稳定性'),
              "shouldersAreFlexible": objs('肩部灵活伸出'),
              "squat": objs('深蹲'),
              "squatOnBackAndForth": objs('前后分腿蹲'),
              "stepOverTheRailings": objs('过栏架步'),
              "torsoStabilityPushUps": objs('躯干稳定性俯撑'),
              "userId": uni.getStorageSync('openid'),
              "userName": uni.getStorageSync('userName')
            }
        console.log('查看大对象', data)
        
        this.$showToast('提交中~', 'loading');
         
        let res = await this.$myRuquest({
          url: '/coach/body/addFMSTestVO',
          method: "POST",
          data,
          header:{
          	'Content-Type':'application/json',
          }
        })
        console.log('查看数据', res)
        uni.navigateTo({
          url: './fms_test_list'
        })
      },
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #F7F5F5 !important;
  }
  .fms-item {
    padding: 20rpx 30rpx;
    background-color: #fff;
    border-bottom: 1px solid #eee;
    >image {
      width: 120rpx;
      height: 100rpx;
      margin-right: 30rpx;
    }
    .abs-emoji {
      position: absolute;
      right: 30rpx;
    }
  }
  textarea {
    padding: 20rpx 30rpx;
    width: 100%;
    height: 350rpx;
    background-color: #fff;
  }
  .bottom-btn {
    width: 100%;
    height: 100rpx;
    background-color: $uni-color-zhuti !important;
    color: #FFFFFF !important;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 99;
  }
</style>
